package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import model.Image;
import dao.ImageDAO;

public class Test {

	public static void main(String[] args) {
		File file = new File(
				"C:\\Documents and Settings\\Ionut\\My Documents\\My Pictures\\__Byakuya_BANKAI___by_AznTranquility.jpg");
		byte[] bfile = new byte[(int) file.length()];
		FileInputStream fileInputStream;
		try {
			fileInputStream = new FileInputStream(file);
			fileInputStream.read(bfile);
			fileInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Image i = new Image();
		i.setIdImage(2);
		i.setIdUser(1);
		i.setImage(bfile);
		i.setImageName("__Byakuya_BANKAI___by_AznTranquility.jpg");

		ImageDAO ir = new ImageDAO();
		ir.addImage(i);

		List<Image> images = ir.findAll();

		for (Iterator<Image> iterator = images.iterator(); iterator.hasNext();) {
			Image image = iterator.next();
			byte[] img = image.getImage();
			System.out.println(image.getIdImage());
			try {

				FileOutputStream fos = new FileOutputStream(
						image.getImageName());
				fos.write(img);
				fos.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
